package com.dms.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.modules.admin.dto.AdminCommentAuditDTO;
import com.dms.modules.admin.dto.AdminCommentReportHandleDTO;
import com.dms.modules.admin.dto.AdminCommentRuleDTO;
import com.dms.modules.admin.dto.AdminSensitiveWordDTO;
import com.dms.modules.admin.service.AdminCommentService;
import com.dms.modules.admin.vo.AdminCommentReportVO;
import com.dms.modules.product.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/admin/comments")
@Api(tags = "管理员评价管理接口")
@PreAuthorize("hasRole('ADMIN')")
public class AdminCommentController {

    @Autowired
    private AdminCommentService adminCommentService;

    @GetMapping("/pending")
    @ApiOperation("获取待审核评论列表")
    public ApiResponse<IPage<CommentVO>> getPendingComments(
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页条数不能小于1") Integer pageSize) {
        Page<CommentVO> page = new Page<>(pageNum, pageSize);
        return ApiResponse.success(adminCommentService.getPendingComments(page));
    }

    @PostMapping("/{commentId}/audit")
    @ApiOperation("审核评论")
    public ApiResponse<Boolean> auditComment(
            @ApiParam("评论ID") @PathVariable @NotNull(message = "评论ID不能为空") Long commentId,
            @ApiParam("审核状态：1-通过 2-拒绝") @RequestParam @NotNull(message = "审核状态不能为空") Integer status,
            @ApiParam("审核备注") @RequestParam(required = false) String remark) {
        return ApiResponse.success(adminCommentService.auditComment(commentId, status, remark));
    }

    @PostMapping("/batch-audit")
    @ApiOperation("批量审核评论")
    public ApiResponse<Boolean> batchAuditComments(@RequestBody @Validated AdminCommentAuditDTO dto) {
        return ApiResponse.success(adminCommentService.batchAuditComments(dto));
    }

    @GetMapping("/reports")
    @ApiOperation("获取评论举报列表")
    public ApiResponse<IPage<AdminCommentReportVO>> getCommentReports(
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页条数不能小于1") Integer pageSize) {
        Page<AdminCommentReportVO> page = new Page<>(pageNum, pageSize);
        return ApiResponse.success(adminCommentService.getCommentReports(page));
    }

    @PostMapping("/reports/handle")
    @ApiOperation("处理评论举报")
    public ApiResponse<Boolean> handleCommentReport(@RequestBody @Validated AdminCommentReportHandleDTO dto) {
        return ApiResponse.success(adminCommentService.handleCommentReport(dto));
    }

    @PostMapping("/sensitive-words")
    @ApiOperation("添加敏感词")
    public ApiResponse<Long> addSensitiveWord(@RequestBody @Validated AdminSensitiveWordDTO dto) {
        return ApiResponse.success(adminCommentService.addSensitiveWord(dto));
    }

    @DeleteMapping("/sensitive-words/{id}")
    @ApiOperation("删除敏感词")
    public ApiResponse<Boolean> deleteSensitiveWord(
            @PathVariable @NotNull(message = "敏感词ID不能为空") Long id) {
        return ApiResponse.success(adminCommentService.deleteSensitiveWord(id));
    }

    @GetMapping("/sensitive-words")
    @ApiOperation("获取敏感词列表")
    public ApiResponse<List<AdminSensitiveWordDTO>> getSensitiveWords() {
        return ApiResponse.success(adminCommentService.getSensitiveWords());
    }

    @PostMapping("/rules")
    @ApiOperation("添加评论规则")
    public ApiResponse<Long> addCommentRule(@RequestBody @Validated AdminCommentRuleDTO dto) {
        return ApiResponse.success(adminCommentService.addCommentRule(dto));
    }

    @PutMapping("/rules/{id}")
    @ApiOperation("更新评论规则")
    public ApiResponse<Boolean> updateCommentRule(
            @PathVariable @NotNull(message = "规则ID不能为空") Long id,
            @RequestBody @Validated AdminCommentRuleDTO dto) {
        return ApiResponse.success(adminCommentService.updateCommentRule(id, dto));
    }

    @DeleteMapping("/rules/{id}")
    @ApiOperation("删除评论规则")
    public ApiResponse<Boolean> deleteCommentRule(
            @PathVariable @NotNull(message = "规则ID不能为空") Long id) {
        return ApiResponse.success(adminCommentService.deleteCommentRule(id));
    }

    @GetMapping("/rules")
    @ApiOperation("获取评论规则列表")
    public ApiResponse<List<AdminCommentRuleDTO>> getCommentRules() {
        return ApiResponse.success(adminCommentService.getCommentRules());
    }

    @PostMapping("/quality-score/{commentId}")
    @ApiOperation("评论质量评分")
    public ApiResponse<Boolean> scoreCommentQuality(
            @PathVariable @NotNull(message = "评论ID不能为空") Long commentId,
            @RequestParam @NotNull(message = "评分不能为空") @Min(value = 1, message = "评分不能小于1") Integer score) {
        return ApiResponse.success(adminCommentService.scoreCommentQuality(commentId, score));
    }
} 